/*------生成随机数------*/
const config = require("./config.js");
const storage = require("./storage.js");

import {
  cart_get,
} from "./api.js";

exports.add = function(id, sellerId, count,salesId) {
  var cart = this.getCache();

  var add = false;
  for (var i = 0; i < cart.length; i++) {
    var item = cart[i];
    if (item.id == id && item.sellerId == sellerId) {
      item.quantity = item.quantity + count;
      add = true;

      if (item.quantity <= 0) {
        cart.splice(i, 1);
      }
      break;
    }
  }

  if (!add && count > 0) {
    var item = {};
    item.id = id;
    item.productId = id;
    item.quantity = count;
    item.sellerId = sellerId;
    item.salesId = salesId;

    cart.push(item);
  }

  this.putCache(cart);

  this.updateCount();
}

exports.getSelected = function() {
  var cart = this.getCache();
  var res = [];
  for (var i = 0; i < cart.length; i++) {
    var item = cart[i];
    if (item.selected) {
      res.push(item);
    }
  }

  return res;
}

exports.getCache = function() {
  var cart = wx.getStorageSync('CART');
  if (!cart || cart == '' || cart == "") {
    cart = [];
  } else {
    cart = JSON.parse(cart);
  }

  return cart;
}

exports.putCache = function(cart) {
  wx.setStorageSync('CART', JSON.stringify(cart));
}

exports.rmCache = function () {
  wx.removeStorageSync('CART');
}

exports.getCount = function() {
  var cart = this.getCache();
  var count = 0;
  for (var i = 0; i < cart.length; i++) {
    var item = cart[i];
    count += item.quantity;
  }

  return count;
}

exports.updateCount = function() {
  var count = this.getCount();
  // if (count > 0) {
  //   wx.setTabBarBadge({
  //     index: 1,
  //     text: count + '',
  //   });
  // } else {
  //   wx.removeTabBarBadge({
  //     index: 1
  //   });
  // }

  return count;
}

exports.requestSelected = function(that, success) {
  var param = {};
  var selected = this.getSelected();
  param.str = JSON.stringify(selected);

  this.request(that, success, param);
}

exports.request = function(that, success, para) {
  var param = {};
  if (para) {
    param = para;
  } else {
    param.str = wx.getStorageSync('CART');
  }

  cart_get(param)
    .then((data) => {
      if (data.success) {
        if (that) {
          var dps = data.result;
          if (dps) {
            for (var i = 0; i < dps.length; i++) {
              var ps = dps[i].products;
              for (var j = 0; j < ps.length; j++) {
                var p = ps[j];
                if (p.cover) {
                  p.cover1 = p.cover.split(',')[0];
                }
              }
            }
          }

          that.setData({
            cartData: dps,
          });

          if (success) {
            success();
          }
        }
      }
    })
}

exports.requestOnlyId = function(that, success, productId, sellerId,quantity) {
  var param = {};
  var obj = {};
  obj.productId = productId;
  obj.id = productId;
  obj.quantity = quantity;
  obj.sellerId = sellerId;

  var array = [obj];

  param.str = JSON.stringify(array);

  this.request(that, success, param);
}

exports.delSelected = function(that, success) {
  var cart = this.getCache();
  var res = [];
  for (var i = 0; i < cart.length; i++) {
    var item = cart[i];
    if (!item.selected) {
      res.push(item);
    }
  }

  this.putCache(res);
}

exports.clear = function (that, success) {
  this.rmCache();
  this.updateCount();
  if(success) {
    success();
  }
}